package com.zjg.vue_cms.security.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjg.vue_cms.interceptor.LoginInterceptor;
import com.zjg.vue_cms.user.dao.TbUserMapper;
import com.zjg.vue_cms.user.entity.TbUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author guass
 * @date 2022年04月24日 10:21
 */
@Slf4j
@Service
public class GuassUserDetailServiceImpl implements UserDetailsService {

    @Resource
    private TbUserMapper tbUserMapper;

    @Autowired
    private LoginInterceptor loginInterceptor;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        log.info("guass {}",loginInterceptor);
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_id",username);

        TbUser user = tbUserMapper.selectOne(wrapper);

        if (user != null) {

            UserDetails details = User.withUsername(username).password(user.getPassword()).roles(user.getRoleType() +"").build();
            return details;
        }

        throw  new UsernameNotFoundException("用户不存在");
    }
}
